/*
 * KKText.h
 *
 *  Created on: 2013-9-15
 *      Author: wanghua
 */

#ifndef KKTEXT_H_
#define KKTEXT_H_

#include "kaka_common.h"
#include "KKNode.h"
#include "KKTTFText.h"
#include "KKFontCache.h"

typedef enum {
	KKTextAlignmentLeft,
	KKTextAlignmentCenter,
	KKTextAlignmentRight,
	KKTextAlignmentTop,
	KKTextAlignmentBottom
} KKTextAlignment;

class KKText: public KKNode,
		public CCSpriteBatchNode,
		public CCLabelProtocol,
		public CCRGBAProtocol {

private:
	;CC_PROPERTY(GLubyte, m_cOpacity, Opacity)
	;CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tColor, Color)
	;CC_PROPERTY(bool, m_bIsOpacityModifyRGB, IsOpacityModifyRGB)
	;CC_PROPERTY(float,m_fFontSize,FontSize)
	;CC_PROPERTY(float,m_wordSpace,WordSpace)
	;CC_PROPERTY(float,m_lineSpace,LineSpace)
	;
	;
protected:
	// string to render
	std::string m_sString;
	KKTextAlignment m_hAlignment;
	KKTextAlignment m_vAlignment;
	KKBitmapFont* m_bitmapFont;

	KKText();

public:

	virtual ~KKText();

	KKNODE_FACTORY_METHOD(KKText,labelWithElement)
	;

	virtual bool isCascadeOpacityEnabled(void) {
		return isCascadeOpacityEnabled();
	}
	virtual GLubyte getDisplayedOpacity(void) {
		return CCLayerRGBA::create()->getDisplayedOpacity();
	}
	void updateDisplayedOpacity(GLubyte opacity) {
		CCLayerRGBA::create()->updateDisplayedOpacity(opacity);
	}
	void setOpacityModifyRGB(bool bValue) {
	}
	virtual bool isOpacityModifyRGB(void) {
		return false;
	}

	virtual bool isCascadeColorEnabled(void) {
		return CCLayerRGBA::create()->isCascadeColorEnabled();
	}
	const ccColor3B& getDisplayedColor(void) {
		return CCLayerRGBA::create()->getDisplayedColor();
	}
	void updateDisplayedColor(const ccColor3B& color) {
		CCLayerRGBA::create()->updateDisplayedColor(color);
	}
	virtual void setCascadeColorEnabled(bool cascadeColorEnabled) {
		CCLayerRGBA::create()->setCascadeColorEnabled(cascadeColorEnabled);
	}
	virtual void setCascadeOpacityEnabled(bool cascadeOpacityEnabled) {
		CCLayerRGBA::create()->setCascadeOpacityEnabled(cascadeOpacityEnabled);
	}

	static KKText* labelWithString(float width, float height, const char *str,
			const char *fntFile, float fontSize, KKTextAlignment hAlignment =
					KKTextAlignmentLeft, KKTextAlignment vAlignment =
					KKTextAlignmentTop);

	virtual bool initCommonInfoWithXml(TiXmlElement* element, KKNode* parent);

	virtual bool initDelayInfoWithXml(TiXmlElement* element);

	virtual void saveToXml(TiXmlElement* element);

	virtual void setIsVisible(bool isVisible) {
		CCSpriteBatchNode::setVisible(isVisible);
	}
	;
	virtual bool getIsVisible() {
		return CCSpriteBatchNode::isVisible();
	}
	;

	bool initWithString1(const char *str, const char *fntFile, float fontSize,
			KKTextAlignment hAlignment = KKTextAlignmentLeft,
			KKTextAlignment vAlignment = KKTextAlignmentTop);

	void createFontChars();

	float measureChars(const char *str);

	virtual void setString(const char *label);
	virtual const char* getString(void);
};

class KKTextParser: public KKNodeParser {
	KKNode* parse(TiXmlElement* element, KKNode* parent) {
		string fontName = XmlUtils::getStringAttr(element, "font-name",
				"default");
		if (fontName.find(".fnt", 0) == fontName.length() - 4) {
			return KKText::labelWithElement(element, parent);
		}

		return KKTTFText::labelWithElement(element, parent);
	}
};
;

#endif /* KKTEXT_H_ */
